﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

/// <summary>
/// PageData 的摘要说明，重载DataList和Repeater控件的分页
/// </summary>
public class PageData
{
    /// <summary>
    /// 小量数据分页类
    /// </summary>
    /// <param name="TB">作用的数据表名</param>
    /// <param name="Sqlstring">分页SQL</param>
    /// <param name="LabCount">显示总记录条数的label</param>
    /// <param name="LabPageCount">显示总页数的label</param>
    /// <param name="LabCurPage">显示当前页的label</param>
    /// <param name="HyLFirPage">第一页</param>
    /// <param name="HyLPrePage">上一页</param>
    /// <param name="HyLNextPage">下一页</param>
    /// <param name="HyLastPage">最后一页</param>
    /// <param name="DL">分页的网格控件，这个是DataList</param>
    /// <param name="number">每页显示的记录数</param>
    public static void Paging(string TB, string Sqlstring, Label LabCount, Label LabPageCount, Label LabCurPage, HyperLink HyLFirPage, HyperLink HyLPrePage, HyperLink HyLNextPage, HyperLink HyLastPage, DataList DL, int number)
    {
        SqlConnection con = new SqlConnection(publicConn.LinkDataBase());
        SqlDataAdapter da = new SqlDataAdapter(Sqlstring, con);
        DataSet ds = new DataSet();
        try
        {
            con.Open();
            da.Fill(ds, TB);
            PagedDataSource objPage = new PagedDataSource();
            objPage.DataSource = ds.Tables[TB].DefaultView;
            LabCount.Text = "总共有" + ds.Tables[TB].Rows.Count.ToString() + "条记录";
            objPage.AllowPaging = true;
            objPage.PageSize = number;
            LabPageCount.Text = "总页数：" + objPage.PageCount.ToString() + "页";
            int CurPage;
            if (HttpContext.Current.Request.QueryString["Page"] != null)
            {
                CurPage = Convert.ToInt32(HttpContext.Current.Request.QueryString["Page"]);
            }
            else
            {
                CurPage = 1;
            }
            objPage.CurrentPageIndex = CurPage - 1;
            LabCurPage.Text = "当前页 ： 第" + CurPage.ToString() + "页";
            if (!objPage.IsFirstPage)
            {
                HyLFirPage.NavigateUrl = HttpContext.Current.Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(1);
                HyLPrePage.NavigateUrl = HttpContext.Current.Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage - 1);
            }
            if (!objPage.IsLastPage)
            {
                HyLNextPage.NavigateUrl = HttpContext.Current.Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage + 1);
                HyLastPage.NavigateUrl = HttpContext.Current.Request.CurrentExecutionFilePath + "?Page=" + objPage.PageCount.ToString();
            }
            DL.DataSource = objPage;
            DL.DataBind();
            con.Close();
        }
        catch (Exception error)
        {
            HttpContext.Current.Response.Write(error.ToString());
        }
    }
    public static void bindData(Repeater rp, string Sqlstring, string TB, Wuqi.Webdiyer.AspNetPager AspNetPager1)
    {
        SqlConnection con = new SqlConnection(publicConn.LinkDataBase());
        con.Open();
        SqlDataAdapter ada = new SqlDataAdapter(Sqlstring, con);
        DataSet ds = new DataSet();
        ada.Fill(ds, AspNetPager1.PageSize * (AspNetPager1.CurrentPageIndex - 1), AspNetPager1.PageSize, TB);
        rp.DataSource = ds.Tables[TB];
        rp.DataBind();
        con.Close();
    }
    /// <summary>
    /// 小量数据分页类
    /// </summary>
    /// <param name="TB">作用的数据表名</param>
    /// <param name="Sqlstring">分页SQL</param>
    /// <param name="LabCount">显示总记录条数的label</param>
    /// <param name="LabPageCount">显示总页数的label</param>
    /// <param name="LabCurPage">显示当前页的label</param>
    /// <param name="HyLFirPage">第一页</param>
    /// <param name="HyLPrePage">上一页</param>
    /// <param name="HyLNextPage">下一页</param>
    /// <param name="HyLastPage">最后一页</param>
    /// <param name="Rp">分页的网格控件，这个是Repeater</param>
    /// <param name="number">每页显示的记录数</param>
    public static void Paging(string TB, string Sqlstring, Label LabCount, Label LabPageCount, Label LabCurPage, HyperLink HyLFirPage, HyperLink HyLPrePage, HyperLink HyLNextPage, HyperLink HyLastPage, Repeater Rp, int number)
    {
        SqlConnection con = new SqlConnection(publicConn.LinkDataBase());
        SqlDataAdapter da = new SqlDataAdapter(Sqlstring, con);
        DataSet ds = new DataSet();
        try
        {
            con.Open();
            da.Fill(ds, TB);
            PagedDataSource objPage = new PagedDataSource();
            objPage.DataSource = ds.Tables[TB].DefaultView;
            LabCount.Text = "总共有" + ds.Tables[TB].Rows.Count.ToString() + "条记录";
            objPage.AllowPaging = true;
            objPage.PageSize = number;
            LabPageCount.Text = "总页数：" + objPage.PageCount.ToString() + "页";
            int CurPage;
            if (HttpContext.Current.Request.QueryString["Page"] != null)
            {
                CurPage = Convert.ToInt32(HttpContext.Current.Request.QueryString["Page"]);
            }
            else
            {
                CurPage = 1;
            }
            objPage.CurrentPageIndex = CurPage - 1;
            LabCurPage.Text = "当前页 ： 第" + CurPage.ToString() + "页";
            if (!objPage.IsFirstPage)
            {
                HyLFirPage.NavigateUrl = HttpContext.Current.Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(1);
                HyLPrePage.NavigateUrl = HttpContext.Current.Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage - 1);
            }
            if (!objPage.IsLastPage)
            {
                HyLNextPage.NavigateUrl = HttpContext.Current.Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage + 1);
                HyLastPage.NavigateUrl = HttpContext.Current.Request.CurrentExecutionFilePath + "?Page=" + objPage.PageCount.ToString();
            }
            Rp.DataSource = objPage;
            Rp.DataBind();
            con.Close();
        }
        catch (Exception error)
        {
            HttpContext.Current.Response.Write(error.ToString());
        }
    }
    //前台调用：
    //<asp:Label ID="LabCount" runat="server" Text="lbCount"></asp:Label>
    //<asp:Label ID="LabPageCount" runat="server" Text="lbTotalPage"></asp:Label>
    //<asp:Label ID="LabCurPage" runat="server" Text="lbCurPage"></asp:Label>
    //<asp:HyperLink ID="HyLFirPage" runat="server">首页</asp:HyperLink>
    //<asp:HyperLink ID="HyLPrePage" runat="server">上一页</asp:HyperLink>
    //<asp:HyperLink ID="HyLNextPage" runat="server">下一页</asp:HyperLink>
    //<asp:HyperLink ID="HyLastPage" runat="server">尾页</asp:HyperLink> 
	public PageData()
	{}
}
